package converters;

import java.util.Iterator;

import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.FacesConverter;

import modelo.Bebida;
import modelo.UnidadMedida;
import services.BebidaService;

@FacesConverter("bebidaConverter")
public class BebidaConverter implements Converter {	
	
	public Object getAsObject(FacesContext fc, UIComponent uic, String value) {
        if(value != null && value.trim().length() > 0) {
            BebidaService service = (BebidaService) fc.getExternalContext().getApplicationMap().get("bebidaService");
//            return service.getListaUnidades().get(Integer.parseInt(value));
             Iterator<Bebida> i=service.getListaBebidas().iterator();
             while (i.hasNext()) {
				Bebida b = (Bebida) i.next();
				if(b.getNombreBebida().compareTo(value)==0)
					return b;
			}
             return null;
        }
        else {
            return null;
        }
    }
 
    public String getAsString(FacesContext fc, UIComponent uic, Object object) {
        if(object != null) {
            return String.valueOf(((Bebida) object).toString());
        }
        else {
            return null;
        }
    } 

}